<template>
  <div id="cate_id">
    <div class="cate_top">
      <div class="cate_add">
        <el-button size="mini" round @click="isShow.flag = true"
          >增加</el-button
        >
      </div>
    </div>
    <el-table
      :data="cate_list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="catename" label="类别名称"> </el-table-column>
      <el-table-column prop="img" label="类别图片" v-slot="$props">
        <template>
          <img :src="static_host + $props.row.img" alt="" class="pic" v-if="$props.row.pid != 0"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" v-slot="$props">
        <template>
          <el-button
            v-if="$props.row.status === 0"
            size="mini"
            type="danger"
            plain
            round
            >禁用</el-button
          >
          <el-button
            v-else-if="$props.row.status === 1"
            size="mini"
            type="success"
            plain
            round
            >正常</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" v-slot="$props">
        <template>
          <el-button size="mini" round @click="updateCate($props.row)"> 修改 </el-button>
        </template>
        <template>
          <el-button size="mini" round @click="delCate($props.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <addCate
      ref="add_cate_ref"
      :isShow="isShow"
      :cate_list="cate_list"
      @reloadCate="reloadCate"
    ></addCate>
  </div>
</template>

<script>
import addCate from "../components/addCate.vue";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      isShow: { flag: false },
    };
  },
  created() {
    this.CateActions();
  },
  methods: {
    
      Del(id){
      var del_url = "/api/catedelete";
      this.$http.post(del_url,{id}).then(res=>{
        if(res.code === 200){
          this.reloadCate()
        }
      })
    },
    delCate(id) {
      this.$confirm("确定要删除商品吗？", "删除商品", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          this.Del(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateCate(obj){
      this.isShow.flag = true
      this.$refs.add_cate_ref.setData(obj)
    },
    reloadCate(){
      this.CateActions()
    },
    ...mapActions("CateStore", ["CateActions"]),
  },
  computed: {
    ...mapState({ cate_list: (state) => state.CateStore.cate_list }),
  },
  components: {
    addCate,
  },
};
</script>

<style>
#cate_id .cate_top {
  display: flex;
  justify-content: space-between;
  flex-direction: row-reverse;
}
#cate_id .el-table {
  margin-top: 20px;
}
#cate_id .cate_add > button {
  width: 80px;
  color: #ff5d3e;
  background-color: white;
}
#cate_id .pic {
  width: 100px;
}
</style>